package com.manjusaka.create6.factory3;

/**
 * 工厂方法模式
 * Created by manjusaka on 2017/6/30.
 * 工厂方法的主要不低是为了解决简单工厂模式代码耦合度高，增加一个或者减少一个类会改变当前的代码结构的问题；
 */
public class FactoryMethod {
    public static void main(String[] args) {
        AbstractLoggerFactory log = new DataBaseLoggerFactory();
        log.writeLog();
    }
}

interface Logger {
    void writeLog();
}

class DataBaseLogger implements Logger {
    public void writeLog() {
        //写日志的方法
        System.out.println("数据库写日志");
    }
}

class FileLogger implements Logger {
    public void writeLog() {
        //写日志的方法
        System.out.println("文件写日志");
    }
}

/**
 * 抽象工厂
 */
interface LoggerFactory {
    Logger creatLogger();
    Logger creatLogger(String[] args);
    Logger creatLogger(Object... obj);
}
abstract class AbstractLoggerFactory implements LoggerFactory{
    public void writeLog() {
        Logger logger = this.creatLogger();
        logger.writeLog();
    }
    public void writeLog(String[] args) {
        Logger logger = this.creatLogger(args);
        logger.writeLog();
    }
    public void writeLog(Object... obj) {
        Logger logger = this.creatLogger(obj);
        logger.writeLog();
    }
}

class DataBaseLoggerFactory extends AbstractLoggerFactory{
    public Logger creatLogger() {
        //创建日志的方法
        System.out.println("创建数据库日志");
        DataBaseLogger DataBaseLogger = new DataBaseLogger();
        return DataBaseLogger;
    }

    public Logger creatLogger(String[] args) {
        //根据参数可以创建一个
        System.out.println("创建数据库日志");
        DataBaseLogger DataBaseLogger = new DataBaseLogger();
        return DataBaseLogger;
    }

    public Logger creatLogger(Object... obj) {
        System.out.println("创建数据库日志");
        DataBaseLogger DataBaseLogger = new DataBaseLogger();
        return DataBaseLogger;
    }
}


class FileLoggerFactory implements LoggerFactory {
    public Logger creatLogger() {
        //创建日志的方法
        System.out.println("创建文件日志");
        FileLogger FileLogger = new FileLogger();
        return FileLogger;
    }

    public Logger creatLogger(String[] args) {
        System.out.println("创建文件日志");
        FileLogger FileLogger = new FileLogger();
        return FileLogger;
    }

    public Logger creatLogger(Object... obj) {
        System.out.println("创建文件日志");
        FileLogger FileLogger = new FileLogger();
        return FileLogger;
    }
}

